package com.example.club.clubmanage.net;
import android.util.Log;


import com.alibaba.fastjson.JSON;
import com.example.club.clubmanage.Constant;

import rx.Subscriber;

/**
 * Created by chenguojun on 16/3/30.
 */
public class MySubscriber<T> extends Subscriber<T> {
    private static final String TAG = "MySubscriber";
    private SubscriberListener subscriberListener;
    private Constant.Type type;
    private String msg;
    public MySubscriber(SubscriberListener subscriberListener, Constant.Type type, String msg) {
        this.subscriberListener = subscriberListener;
        this.type = type;
        this.msg = msg;
    }
    public MySubscriber(SubscriberListener subscriberListener, Constant.Type type) {
        this(subscriberListener,type,"");
    }
    @Override
    public void onCompleted() {
        Log.d(TAG,"onCompleted:"+type.name());
        subscriberListener.onCompleted(type,msg);
    }

    @Override
    public void onError(Throwable e) {
        e.printStackTrace();
        Log.d(TAG, "onError--"+type.name()+":"+e.toString());
        if (e.toString().contains("java.net.SocketTimeoutException")) {// 请求超时
            Log.d(TAG,type.name()+" 请求超时");
        } else if ( e.toString().contains("retrofit2.adapter.rxjava.HttpException")) { // 没有该接口
            Log.d(TAG,type.name()+" 没有该接口");
        } else if (e.toString().contains("com.google.gson.JsonSyntaxException")) {
            Log.d(TAG,type.name()+" 类型转换错误");
        }
        try {
            subscriberListener.onError(e,type,msg);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
    @Override
    public void onNext(Object o) {
        String jsonString = JSON.toJSONString(o);
        Log.d(TAG,"onNext--"+type.name()+":"+jsonString);
        if (subscriberListener != null) {
            subscriberListener.onNext(o, type,msg);
        }
    }
}
